package puf.m2.automaton;

import java.io.Serializable;

/**
 * A <CODE>Transition</CODE> object represents a
 * transition between two state objects in an automaton
 */
public class Transition implements Serializable {

    private static final long serialVersionUID = 1L;

    /** From state */
    private State source;
    /** To state */
    private State target;
    /** Transition's symbol */
    private String label;
    /* positive means Right, read Left to Right */
    private Direction direction;

    // default direction of a transition is positive
    public Transition(State source, State target, String label) {
        this(source, target, label, Direction.Positive);
    }

    public Transition(State source, State target, String label, Direction direction) {
        this.source = source;
        this.target = target;
        this.label = label;
        this.direction = direction;
    }

    /**
     * Returns a string representation of this object. The string returned is
     * the string representation of the first state, the string
     * representation of the second state, and the label.
     * @return a string representation of this object
     */
    public String toString() {
        return "[" + source().toString() + "] -> [" +
                target().toString() + "] with label " + label.toString();
    }

    /**
     * Returns if this transition equals another object.
     * @param object
     *            the object to test against
     * @return <CODE>true</CODE> if the two are equal, <CODE>false</CODE>
     *         otherwise
     */
    public boolean equals(Object object) {
        if (!(object instanceof Transition))
            return false;

        Transition t = (Transition) object;
        return source == t.source && target == t.target;
    }

    /*--------- Getter/Setter ---------*/
    public State source() {
        return source;
    }

    public void setSource(State state) {
        this.source = state;
    }

    public State target() {
        return target;
    }

    public void setTarget(State state) {
        this.target = state;
    }

    public String label() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public Direction direction() {
        return direction;
    }

    /**
     * Returns the automaton this transition is over.
     * @return the automaton
     */
    public Automaton getAutomaton() {
        return this.source.getAutomaton();
    }
}
